package com.xiaozhichao.databafflesys.admin.config;

import com.xiaozhichao.databafflesys.admin.controller.BaseController;
import com.xiaozhichao.databafflesys.common.domain.ExceptionEnums;
import com.xiaozhichao.databafflesys.common.domain.ResultVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class GlobalExceptionHandler extends BaseController {

	@ExceptionHandler({ RuntimeException.class, Exception.class })
	@ResponseBody
	public ResultVo exceptionHandler(Exception e, HttpServletResponse response) {
		this.logger.error("GlobalExceptionHandler.exceptionHandler:", e);
		if(e instanceof HttpMessageNotReadableException){
			return ResultVo.getResult(ExceptionEnums.REQEUST_IS_NOT_NULL);
		}
		if(StringUtils.isEmpty(e.getMessage())){
			return ResultVo.getResult(ExceptionEnums.SYSTEM_ERROR);
		}
		return ResultVo.getResult(ExceptionEnums.SYSTEM_ERROR , e.getMessage());
	}

}